Imports System.IO

Imports Phoenix.DataAccess
Imports Phoenix.Events
Imports Phoenix.Forms.Controls
Imports Phoenix.Forms.Dialogs
Imports Phoenix.Helpers
Imports Phoenix.Preferences

Namespace Forms
    Public Class AppointmentForm : Inherits ChildFormBase

#Region " Private Fields "
        Private dsApp As DataSetAppointments
#End Region

#Region " BarManager Items "
        Friend WithEvents CreateBillBarItem As DevExpress.XtraBars.BarButtonItem
        Friend WithEvents RecurrenceBarItem As DevExpress.XtraBars.BarButtonItem
        Friend WithEvents SqueezeBarItem As DevExpress.XtraBars.BarButtonItem

        Private Sub CustomInitializeComponent()
            Dim pinfo As DevExpress.XtraBars.LinkPersistInfo

            ActionsMenuBarItem.Visibility = DevExpress.XtraBars.BarItemVisibility.Always

            'CreateBillBarItem
            Me.CreateBillBarItem = New DevExpress.XtraBars.BarButtonItem
            Me.BarManager.Items.Add(Me.CreateBillBarItem)

            Me.StandardBar.LinksPersistInfo.Add(New DevExpress.XtraBars.LinkPersistInfo(Me.CreateBillBarItem, True))

            Me.ActionsMenuBarItem.LinksPersistInfo.Add(New DevExpress.XtraBars.LinkPersistInfo(Me.CreateBillBarItem))

            Me.CreateBillBarItem.Caption = "Create &Bill"
            Me.CreateBillBarItem.CategoryGuid = New System.Guid("c3aced69-cd2a-4d58-8a80-811fa2689282")
            Me.CreateBillBarItem.Id = 100
            Me.CreateBillBarItem.Name = "CreateBillBarItem"
            Me.CreateBillBarItem.ImageIndex = 27

            'RecurrenceBarItem
            Me.RecurrenceBarItem = New DevExpress.XtraBars.BarButtonItem
            Me.BarManager.Items.Add(Me.RecurrenceBarItem)

            Me.StandardBar.LinksPersistInfo.Add(New DevExpress.XtraBars.LinkPersistInfo(Me.RecurrenceBarItem, True))

            Me.ActionsMenuBarItem.LinksPersistInfo.Add(New DevExpress.XtraBars.LinkPersistInfo(Me.RecurrenceBarItem))

            Me.RecurrenceBarItem.Caption = "&Recurrence..."
            Me.RecurrenceBarItem.CategoryGuid = New System.Guid("c3aced69-cd2a-4d58-8a80-811fa2689282")
            Me.RecurrenceBarItem.Id = 101
            Me.RecurrenceBarItem.Name = "RecurrenceBarItem"
            'Me.CreateBillBarItem.ImageIndex = -1

            'SqueezeBarItem
            Me.SqueezeBarItem = New DevExpress.XtraBars.BarButtonItem
            Me.BarManager.Items.Add(Me.SqueezeBarItem)

            Me.StandardBar.LinksPersistInfo.Add(New DevExpress.XtraBars.LinkPersistInfo(Me.SqueezeBarItem, True))

            Me.ActionsMenuBarItem.LinksPersistInfo.Add(New DevExpress.XtraBars.LinkPersistInfo(Me.SqueezeBarItem))

            Me.SqueezeBarItem.Caption = "&Squeeze"
            Me.SqueezeBarItem.CategoryGuid = New System.Guid("c3aced69-cd2a-4d58-8a80-811fa2689282")
            Me.SqueezeBarItem.Id = 101
            Me.SqueezeBarItem.Name = "SqueezeBarItem"

        End Sub

        Private Sub RecurrenceBarItem_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles RecurrenceBarItem.ItemClick
            If DataValidate() Then
                Dim dialog As New RepeatedAppointmentDialog
                dialog.Init(DataSource.StartTime, DataSource)
                dialog.ShowDialog()
            End If
        End Sub

        Private Sub SqueezeBarItem_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles SqueezeBarItem.ItemClick
            If Not MyBase.DataValidate() Then Return

            Dim st As DateTime = DataSource.StartTime
            Dim ft As DateTime = DataSource.FinishTime

            If Not Helpers.AppointmentHelper.IsValidSalonTime(DataSource) Then
                DataSource.StartTime = st
                DataSource.FinishTime = ft
                AppMessageBox(Res.AppointmentNotInSalonTime)
                Return
            End If
            If Not Helpers.AppointmentHelper.IsValidEmployeeTime(DataSource) Then
                DataSource.StartTime = st
                DataSource.FinishTime = ft
                AppMessageBox(Res.AppointmentNotInStaffMemberFreeTime)
                Return
            End If
            If Not Helpers.AppointmentHelper.IsValidClientTime(DataSource) Then
                DataSource.StartTime = st
                DataSource.FinishTime = ft
                AppMessageBox(Res.AppointmentNotInClientFreeTime)
                Return
            End If

            If DataSource.StartTime.CompareTo(st) <> 0 Or DataSource.FinishTime.CompareTo(ft) <> 0 Then
                If AppointmentHelper.AskToSqueezeAppointment(st, ft, DataSource) Then
                    StartTime.Time = DataSource.StartTime
                    FinishTime.Time = DataSource.FinishTime
                Else
                    DataSource.StartTime = st
                    DataSource.FinishTime = ft
                End If
            End If
        End Sub

        Private Sub CreateBillBarItem_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles CreateBillBarItem.ItemClick
            Dim cID As Integer = DataSource.ClientID

            SaveAndClose()
            If Not Visible Then
                BillForm.OpenForm(GetType(DataSetBill.Bill), cID)
            End If
        End Sub

#End Region

#Region " Windows Form Designer generated code "

        Public Sub New()
            MyBase.New()

            SuspendDataChangedEvents = True

            'This call is required by the Windows Form Designer.
            InitializeComponent()
            CustomInitializeComponent()
            StatusCombo.Enabled = False

            SuspendDataChangedEvents = False
        End Sub

        'UserControl overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Friend WithEvents DescriptionBox As DevExpress.XtraEditors.TextEdit
        'Friend WithEvents RepeatingBarItem As DevExpress.XtraBars.BarButtonItem
        Friend WithEvents ErrorProvider As System.Windows.Forms.ErrorProvider
        Friend WithEvents DescriptionLabel As System.Windows.Forms.Label
        Friend WithEvents Belevel1 As System.Windows.Forms.GroupBox
        Friend WithEvents Belevel2 As System.Windows.Forms.GroupBox
        Friend WithEvents ClientLabel As System.Windows.Forms.Label
        Friend WithEvents ServiceLabel As System.Windows.Forms.Label
        Friend WithEvents PerformerLabel As System.Windows.Forms.Label
        Friend WithEvents DateLabel As System.Windows.Forms.Label
        Friend WithEvents StatusCombo As DevExpress.XtraEditors.ComboBoxEdit
        Friend WithEvents StatusLabel As System.Windows.Forms.Label
        Friend WithEvents StartTimeLabel As System.Windows.Forms.Label
        Friend WithEvents FinishTimeLabel As System.Windows.Forms.Label
        Friend WithEvents DurationLabel As System.Windows.Forms.Label
        Friend WithEvents DurationInfo As System.Windows.Forms.Label
        Friend WithEvents CommentMemo As DevExpress.XtraEditors.MemoEdit
        Friend WithEvents OwnerInfo As System.Windows.Forms.Label
        Friend WithEvents ContentPane As System.Windows.Forms.Panel
        Friend WithEvents Belevel3 As System.Windows.Forms.GroupBox
        Friend WithEvents AppointmentDate As DevExpress.XtraEditors.DateEdit
        Friend WithEvents StartTime As DevExpress.XtraEditors.TimeEdit
        Friend WithEvents ClientControl As Phoenix.Forms.Controls.AppointmentClientEdit
        Friend WithEvents ServiceControl As Phoenix.Forms.Controls.AppointmentServiceEdit
        Friend WithEvents EmployeeControl As Phoenix.Forms.Controls.EmployeeFieldEdit
        Friend WithEvents FinishTime As DevExpress.XtraEditors.TimeEdit
        Friend WithEvents DurationTime As DevExpress.XtraEditors.SpinEdit
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(AppointmentForm))
            Me.ContentPane = New System.Windows.Forms.Panel
            Me.DurationTime = New DevExpress.XtraEditors.SpinEdit
            Me.FinishTime = New DevExpress.XtraEditors.TimeEdit
            Me.OwnerInfo = New System.Windows.Forms.Label
            Me.CommentMemo = New DevExpress.XtraEditors.MemoEdit
            Me.Belevel3 = New System.Windows.Forms.GroupBox
            Me.DurationInfo = New System.Windows.Forms.Label
            Me.DurationLabel = New System.Windows.Forms.Label
            Me.FinishTimeLabel = New System.Windows.Forms.Label
            Me.StatusCombo = New DevExpress.XtraEditors.ComboBoxEdit
            Me.StatusLabel = New System.Windows.Forms.Label
            Me.StartTime = New DevExpress.XtraEditors.TimeEdit
            Me.StartTimeLabel = New System.Windows.Forms.Label
            Me.AppointmentDate = New DevExpress.XtraEditors.DateEdit
            Me.DateLabel = New System.Windows.Forms.Label
            Me.Belevel2 = New System.Windows.Forms.GroupBox
            Me.EmployeeControl = New Phoenix.Forms.Controls.EmployeeFieldEdit
            Me.PerformerLabel = New System.Windows.Forms.Label
            Me.ServiceLabel = New System.Windows.Forms.Label
            Me.ServiceControl = New Phoenix.Forms.Controls.AppointmentServiceEdit
            Me.ClientLabel = New System.Windows.Forms.Label
            Me.ClientControl = New Phoenix.Forms.Controls.AppointmentClientEdit
            Me.DescriptionLabel = New System.Windows.Forms.Label
            Me.DescriptionBox = New DevExpress.XtraEditors.TextEdit
            Me.Belevel1 = New System.Windows.Forms.GroupBox
            Me.ErrorProvider = New System.Windows.Forms.ErrorProvider
            Me.ContentPane.SuspendLayout()
            CType(Me.DurationTime.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.FinishTime.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.CommentMemo.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.StatusCombo.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.StartTime.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.AppointmentDate.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.DescriptionBox.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
            Me.SuspendLayout()
            '
            'ContentPane
            '
            Me.ContentPane.Controls.Add(Me.DurationTime)
            Me.ContentPane.Controls.Add(Me.FinishTime)
            Me.ContentPane.Controls.Add(Me.OwnerInfo)
            Me.ContentPane.Controls.Add(Me.CommentMemo)
            Me.ContentPane.Controls.Add(Me.Belevel3)
            Me.ContentPane.Controls.Add(Me.DurationInfo)
            Me.ContentPane.Controls.Add(Me.DurationLabel)
            Me.ContentPane.Controls.Add(Me.FinishTimeLabel)
            Me.ContentPane.Controls.Add(Me.StatusCombo)
            Me.ContentPane.Controls.Add(Me.StatusLabel)
            Me.ContentPane.Controls.Add(Me.StartTime)
            Me.ContentPane.Controls.Add(Me.StartTimeLabel)
            Me.ContentPane.Controls.Add(Me.AppointmentDate)
            Me.ContentPane.Controls.Add(Me.DateLabel)
            Me.ContentPane.Controls.Add(Me.Belevel2)
            Me.ContentPane.Controls.Add(Me.EmployeeControl)
            Me.ContentPane.Controls.Add(Me.PerformerLabel)
            Me.ContentPane.Controls.Add(Me.ServiceLabel)
            Me.ContentPane.Controls.Add(Me.ServiceControl)
            Me.ContentPane.Controls.Add(Me.ClientLabel)
            Me.ContentPane.Controls.Add(Me.ClientControl)
            Me.ContentPane.Controls.Add(Me.DescriptionLabel)
            Me.ContentPane.Controls.Add(Me.DescriptionBox)
            Me.ContentPane.Controls.Add(Me.Belevel1)
            Me.ContentPane.Dock = System.Windows.Forms.DockStyle.Fill
            Me.ContentPane.Location = New System.Drawing.Point(0, 49)
            Me.ContentPane.Name = "ContentPane"
            Me.ContentPane.Size = New System.Drawing.Size(632, 469)
            Me.ContentPane.TabIndex = 4
            '
            'DurationTime
            '
            Me.DurationTime.EditValue = 0
            Me.DurationTime.Location = New System.Drawing.Point(504, 152)
            Me.DurationTime.Name = "DurationTime"
            '
            'DurationTime.Properties
            '
            Me.DurationTime.Properties.Buttons.AddRange(New DevExpress.XtraEditors.Controls.EditorButton() {New DevExpress.XtraEditors.Controls.EditorButton})
            Me.DurationTime.Properties.DisplayFormat.FormatString = "{0} mins"
            Me.DurationTime.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric
            Me.DurationTime.Properties.Increment = New Decimal(New Integer() {5, 0, 0, 0})
            Me.DurationTime.Properties.UseCtrlIncrement = False
            Me.DurationTime.Size = New System.Drawing.Size(64, 20)
            Me.DurationTime.TabIndex = 36
            '
            'FinishTime
            '
            Me.FinishTime.EditValue = New Date(2005, 4, 10, 0, 0, 0, 0)
            Me.FinishTime.Location = New System.Drawing.Point(328, 176)
            Me.FinishTime.Name = "FinishTime"
            '
            'FinishTime.Properties
            '
            Me.FinishTime.Properties.Buttons.AddRange(New DevExpress.XtraEditors.Controls.EditorButton() {New DevExpress.XtraEditors.Controls.EditorButton})
            Me.FinishTime.Properties.TimeFormat = DevExpress.XtraEditors.Controls.TimeFormat.HourMin
            Me.FinishTime.Properties.UseCtrlIncrement = False
            Me.FinishTime.Size = New System.Drawing.Size(72, 20)
            Me.FinishTime.TabIndex = 35
            '
            'OwnerInfo
            '
            Me.OwnerInfo.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
                        Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            Me.OwnerInfo.Location = New System.Drawing.Point(8, 448)
            Me.OwnerInfo.Name = "OwnerInfo"
            Me.OwnerInfo.Size = New System.Drawing.Size(616, 16)
            Me.OwnerInfo.TabIndex = 34
            '
            'CommentMemo
            '
            Me.CommentMemo.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                        Or System.Windows.Forms.AnchorStyles.Left) _
                        Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            Me.CommentMemo.EditValue = ""
            Me.CommentMemo.Location = New System.Drawing.Point(8, 224)
            Me.CommentMemo.Name = "CommentMemo"
            Me.CommentMemo.Size = New System.Drawing.Size(616, 218)
            Me.CommentMemo.TabIndex = 16
            '
            'Belevel3
            '
            Me.Belevel3.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                        Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            Me.Belevel3.Location = New System.Drawing.Point(8, 208)
            Me.Belevel3.Name = "Belevel3"
            Me.Belevel3.Size = New System.Drawing.Size(616, 2)
            Me.Belevel3.TabIndex = 32
            Me.Belevel3.TabStop = False
            '
            'DurationInfo
            '
            Me.DurationInfo.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, CType(204, Byte))
            Me.DurationInfo.Location = New System.Drawing.Point(448, 178)
            Me.DurationInfo.Name = "DurationInfo"
            Me.DurationInfo.Size = New System.Drawing.Size(160, 16)
            Me.DurationInfo.TabIndex = 31
            Me.DurationInfo.Text = "Expected duration: 20 mins"
            Me.DurationInfo.Visible = False
            '
            'DurationLabel
            '
            Me.DurationLabel.Location = New System.Drawing.Point(448, 154)
            Me.DurationLabel.Name = "DurationLabel"
            Me.DurationLabel.Size = New System.Drawing.Size(56, 16)
            Me.DurationLabel.TabIndex = 14
            Me.DurationLabel.Text = "Du&ration :"
            '
            'FinishTimeLabel
            '
            Me.FinishTimeLabel.Location = New System.Drawing.Point(264, 178)
            Me.FinishTimeLabel.Name = "FinishTimeLabel"
            Me.FinishTimeLabel.Size = New System.Drawing.Size(56, 16)
            Me.FinishTimeLabel.TabIndex = 12
            Me.FinishTimeLabel.Text = "&Finish on:"
            '
            'StatusCombo
            '
            Me.StatusCombo.EditValue = ""
            Me.StatusCombo.Location = New System.Drawing.Point(96, 176)
            Me.StatusCombo.Name = "StatusCombo"
            '
            'StatusCombo.Properties
            '
            Me.StatusCombo.Properties.Buttons.AddRange(New DevExpress.XtraEditors.Controls.EditorButton() {New DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)})
            Me.StatusCombo.Size = New System.Drawing.Size(144, 20)
            Me.StatusCombo.TabIndex = 9
            '
            'StatusLabel
            '
            Me.StatusLabel.Location = New System.Drawing.Point(16, 178)
            Me.StatusLabel.Name = "StatusLabel"
            Me.StatusLabel.Size = New System.Drawing.Size(80, 16)
            Me.StatusLabel.TabIndex = 8
            Me.StatusLabel.Text = "Stat&us :"
            '
            'StartTime
            '
            Me.StartTime.EditValue = New Date(2005, 4, 10, 0, 0, 0, 0)
            Me.StartTime.Location = New System.Drawing.Point(328, 152)
            Me.StartTime.Name = "StartTime"
            '
            'StartTime.Properties
            '
            Me.StartTime.Properties.Buttons.AddRange(New DevExpress.XtraEditors.Controls.EditorButton() {New DevExpress.XtraEditors.Controls.EditorButton})
            Me.StartTime.Properties.TimeFormat = DevExpress.XtraEditors.Controls.TimeFormat.HourMin
            Me.StartTime.Properties.UseCtrlIncrement = False
            Me.StartTime.Size = New System.Drawing.Size(72, 20)
            Me.StartTime.TabIndex = 11
            '
            'StartTimeLabel
            '
            Me.StartTimeLabel.Location = New System.Drawing.Point(264, 154)
            Me.StartTimeLabel.Name = "StartTimeLabel"
            Me.StartTimeLabel.Size = New System.Drawing.Size(56, 16)
            Me.StartTimeLabel.TabIndex = 10
            Me.StartTimeLabel.Text = "S&tart on:"
            '
            'AppointmentDate
            '
            Me.AppointmentDate.EditValue = New Date(2005, 4, 10, 0, 0, 0, 0)
            Me.AppointmentDate.Location = New System.Drawing.Point(96, 152)
            Me.AppointmentDate.Name = "AppointmentDate"
            '
            'AppointmentDate.Properties
            '
            Me.AppointmentDate.Properties.Buttons.AddRange(New DevExpress.XtraEditors.Controls.EditorButton() {New DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)})
            Me.AppointmentDate.Size = New System.Drawing.Size(144, 20)
            Me.AppointmentDate.TabIndex = 7
            '
            'DateLabel
            '
            Me.DateLabel.Location = New System.Drawing.Point(16, 154)
            Me.DateLabel.Name = "DateLabel"
            Me.DateLabel.Size = New System.Drawing.Size(80, 16)
            Me.DateLabel.TabIndex = 6
            Me.DateLabel.Text = "&Date:"
            '
            'Belevel2
            '
            Me.Belevel2.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                        Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            Me.Belevel2.Location = New System.Drawing.Point(8, 136)
            Me.Belevel2.Name = "Belevel2"
            Me.Belevel2.Size = New System.Drawing.Size(616, 2)
            Me.Belevel2.TabIndex = 20
            Me.Belevel2.TabStop = False
            '
            'EmployeeControl
            '
            Me.EmployeeControl.EditValue = Nothing
            Me.EmployeeControl.Location = New System.Drawing.Point(96, 104)
            Me.EmployeeControl.Name = "EmployeeControl"
            Me.EmployeeControl.Size = New System.Drawing.Size(304, 20)
            Me.EmployeeControl.TabIndex = 5
            '
            'PerformerLabel
            '
            Me.PerformerLabel.Location = New System.Drawing.Point(16, 105)
            Me.PerformerLabel.Name = "PerformerLabel"
            Me.PerformerLabel.Size = New System.Drawing.Size(80, 16)
            Me.PerformerLabel.TabIndex = 4
            Me.PerformerLabel.Text = "Staff &Member :"
            '
            'ServiceLabel
            '
            Me.ServiceLabel.Location = New System.Drawing.Point(16, 82)
            Me.ServiceLabel.Name = "ServiceLabel"
            Me.ServiceLabel.Size = New System.Drawing.Size(80, 16)
            Me.ServiceLabel.TabIndex = 2
            Me.ServiceLabel.Text = "&Service:"
            '
            'ServiceControl
            '
            Me.ServiceControl.EditValue = Nothing
            Me.ServiceControl.Location = New System.Drawing.Point(96, 80)
            Me.ServiceControl.Name = "ServiceControl"
            Me.ServiceControl.Size = New System.Drawing.Size(304, 20)
            Me.ServiceControl.TabIndex = 3
            '
            'ClientLabel
            '
            Me.ClientLabel.Location = New System.Drawing.Point(16, 58)
            Me.ClientLabel.Name = "ClientLabel"
            Me.ClientLabel.Size = New System.Drawing.Size(80, 16)
            Me.ClientLabel.TabIndex = 0
            Me.ClientLabel.Text = "&Client:"
            '
            'ClientControl
            '
            Me.ClientControl.EditValue = Nothing
            Me.ClientControl.Location = New System.Drawing.Point(96, 56)
            Me.ClientControl.Name = "ClientControl"
            Me.ClientControl.Size = New System.Drawing.Size(304, 20)
            Me.ClientControl.TabIndex = 1
            '
            'DescriptionLabel
            '
            Me.DescriptionLabel.Location = New System.Drawing.Point(16, 13)
            Me.DescriptionLabel.Name = "DescriptionLabel"
            Me.DescriptionLabel.Size = New System.Drawing.Size(80, 16)
            Me.DescriptionLabel.TabIndex = 0
            Me.DescriptionLabel.Text = "&Description:"
            '
            'DescriptionBox
            '
            Me.DescriptionBox.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                        Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            Me.DescriptionBox.EditValue = ""
            Me.DescriptionBox.Location = New System.Drawing.Point(96, 11)
            Me.DescriptionBox.Name = "DescriptionBox"
            Me.DescriptionBox.Size = New System.Drawing.Size(512, 20)
            Me.DescriptionBox.TabIndex = 1
            '
            'Belevel1
            '
            Me.Belevel1.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                        Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            Me.Belevel1.Location = New System.Drawing.Point(8, 40)
            Me.Belevel1.Name = "Belevel1"
            Me.Belevel1.Size = New System.Drawing.Size(616, 2)
            Me.Belevel1.TabIndex = 13
            Me.Belevel1.TabStop = False
            '
            'ErrorProvider
            '
            Me.ErrorProvider.ContainerControl = Me
            '
            'AppointmentForm
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(632, 518)
            Me.Controls.Add(Me.ContentPane)
            Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
            Me.MinimumSize = New System.Drawing.Size(600, 400)
            Me.Name = "AppointmentForm"
            Me.Text = "Edit Appointment"
            Me.Controls.SetChildIndex(Me.ContentPane, 0)
            Me.ContentPane.ResumeLayout(False)
            CType(Me.DurationTime.Properties, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.FinishTime.Properties, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.CommentMemo.Properties, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.StatusCombo.Properties, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.StartTime.Properties, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.AppointmentDate.Properties, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.DescriptionBox.Properties, System.ComponentModel.ISupportInitialize).EndInit()
            Me.ResumeLayout(False)

        End Sub

#End Region

#Region " Data "
        Public Shadows Property DataSource() As DataSetAppointments.Appointment
            Get
                Return CType(MyBase.DataSource, DataSetAppointments.Appointment)
            End Get
            Set(ByVal Value As DataSetAppointments.Appointment)
                MyBase.DataSource = Value
            End Set
        End Property

        Protected Overrides Sub DoBinding()
            SuspendDataChangedEvents = True

            Dim bind As Binding

            'Description
            DescriptionBox.DataBindings.Add("Text", DataSource, "Description")

            'Comment
            CommentMemo.DataBindings.Add("Text", DataSource, "Comment")

            'Service/Employee/Client
            ServiceControl.EditValue = AppDataLayer.Services.FindByServiceID(DataSource.ServiceID)
            EmployeeControl.EditValue = AppDataLayer.Employees.FindByEmployeeID(DataSource.PerformerID)
            If (ServiceControl.EditValue Is Nothing Or EmployeeControl.EditValue Is Nothing) Then
                FinishTime.Enabled = False
                DurationTime.Enabled = False
            End If
            ClientControl.EditValue = AppDataLayer.Clients.FindByClientID(DataSource.ClientID)


            'Created ...
            If Helpers.AppointmentHelper.IsNew(DataSource) Then
                OwnerInfo.Text = String.Empty
            Else
                Dim emp As DataSetData.Employee = AppDataLayer.Employees.FindByEmployeeID(DataSource.OperatorID)

                If emp Is Nothing Then
                    OwnerInfo.Text = String.Format(Res.ObjectOwnerInfo, Res.UnknownOwnerTitle, DataSource.CreationTime)
                Else
                    OwnerInfo.Text = String.Format(Res.ObjectOwnerInfo, emp.FullName, DataSource.CreationTime)
                End If
            End If

            'Times
            UpdateTimesOnForm()

            'Status
            Dim status As AppointmentStatus = AppointmentHelper.GetAppointmentStatus(DataSource)
            StatusCombo.Text = Helpers.AppointmentHelper.GetAppointmentStatusString(status)
            If (status = AppointmentStatus.asPaid) Then
                ClientControl.Enabled = False
                ServiceControl.Enabled = False
                EmployeeControl.Enabled = False
                DescriptionBox.Properties.ReadOnly = True
                AppointmentDate.Properties.ReadOnly = True
                StartTime.Properties.ReadOnly = True
                FinishTime.Properties.ReadOnly = True
                CommentMemo.Properties.ReadOnly = True
                DurationTime.Properties.ReadOnly = True
            End If

            SuspendDataChangedEvents = False
        End Sub


        Public Overrides ReadOnly Property DocumentTitle() As String
            Get
                Return DataSource.Description
            End Get
        End Property

        Public Overrides ReadOnly Property DocumentType() As String
            Get
                Return Res.TypeAppointmentDocument
            End Get
        End Property

#End Region

#Region " Event Handlers "
        Private Sub AppointmentForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.ResizeRedraw = False

            AddControlToValidate(ServiceControl, AddressOf ServiceControl_DataValidating)
            AddControlToValidate(ClientControl, AddressOf ClientControl_DataValidating)
            AddControlToValidate(EmployeeControl, AddressOf EmployeeControl_DataValidating)
            AddControlToValidate(DurationTime, AddressOf DurationTime_DataValidating)

        End Sub

#End Region

#Region " Life-Cycle Routines "
        Protected Overrides Sub OnDelete()
            AppointmentHelper.DeleteAppointment(DataSource, Me)
        End Sub

        Protected Overrides Sub DoSaving()
            Helpers.AppointmentHelper.UpdateAppointment(DataSource, Me)
        End Sub

        Protected Overrides Function CreateNewRow() As DataRow
            Return Helpers.AppointmentHelper.NewAppointment(dsApp)
        End Function

        Protected Overrides Sub OnApplicationEvent(ByVal sender As Object, ByVal e As Events.PhoenixEventArgs)
            If TypeOf e.EventData Is DataSetData.Client Then
                If (e.EventCode = PhoenixEventCode.BizRemoved) Then
                    Dim client As DataSetData.Client = e.EventData
                    If (DataSource.RowState = DataRowState.Detached) Then
                        If client.ClientID = DataSource.ClientID Then
                            DataSource.ClientID = 0
                            ClientControl.EditValue = Nothing
                        End If
                    Else
                        ForceClose()
                    End If
                End If
            ElseIf TypeOf e.EventData Is DataSetData.Service Then
                If (e.EventCode = PhoenixEventCode.BizRemoved) Then
                    Dim service As DataSetData.Service = e.EventData
                    If (DataSource.RowState = DataRowState.Detached) Then
                        If service.ServiceID = DataSource.ServiceID Then
                            DataSource.ServiceID = 0
                            ServiceControl.EditValue = Nothing
                        End If
                    Else
                        ForceClose()
                    End If
                End If
            ElseIf TypeOf e.EventData Is DataSetData.Employee Then
                If e.EventCode = PhoenixEventCode.BizRemoved Then
                    Dim em As DataSetData.Employee = e.EventData
                    If (DataSource.RowState = DataRowState.Detached) Then
                        If em.EmployeeID = DataSource.PerformerID Then
                            DataSource.PerformerID = 0
                            EmployeeControl.EditValue = Nothing
                        End If
                    Else
                        ForceClose()
                    End If
                End If
            ElseIf TypeOf e.EventData Is DataSetAppointments.Appointment Then
                Dim app As DataSetAppointments.Appointment = CType(e.EventData, DataSetAppointments.Appointment)
                If DataSource.RowState <> DataRowState.Detached Then
                    If app.AppointmentID = DataSource.AppointmentID Then
                        If e.EventCode = PhoenixEventCode.BizRemoved Then
                            ForceClose()
                        ElseIf e.EventCode = PhoenixEventCode.BizUpdated Then
                            DataSource = app
                        End If
                    End If
                Else
                    If app Is DataSource Then
                        If e.EventCode = PhoenixEventCode.BizRemoved Then
                            ForceClose()
                        End If
                    End If
                End If
            End If
        End Sub

#End Region

#Region " Preferenses "
        Protected Overrides Sub LoadPreferences(ByVal pref As IPreferencesNode)
            MyBase.LoadPreferences(pref)

            Dim node As IPreferencesNode
            Dim ba As Byte()

            ' clientControl
            node = pref.GetNodeForPath(ClientControl.Name)
            ba = node.GetByteArray("Grid", Nothing) : If Not ba Is Nothing Then ClientControl.GridView.RestoreLayoutFromStream(New MemoryStream(ba))
            ClientControl.PopupWindow.Width = node.GetInt32("PopupWidth", ClientControl.PopupWindow.Width)
            ClientControl.PopupWindow.Height = node.GetInt32("PopupHeight", ClientControl.PopupWindow.Height)
            ClientControl.UpdateConfiguration()


            ' employeeControl
            node = pref.GetNodeForPath(EmployeeControl.Name)
            ba = node.GetByteArray("Grid", Nothing) : If Not ba Is Nothing Then EmployeeControl.GridView.RestoreLayoutFromStream(New MemoryStream(ba))
            EmployeeControl.PopupWindow.Width = node.GetInt32("PopupWidth", EmployeeControl.PopupWindow.Width)
            EmployeeControl.PopupWindow.Height = node.GetInt32("PopupHeight", EmployeeControl.PopupWindow.Height)
            EmployeeControl.UpdateConfiguration()


            ' serviceControl
            node = pref.GetNodeForPath(ServiceControl.Name)
            ba = node.GetByteArray("Grid", Nothing) : If Not ba Is Nothing Then ServiceControl.GridView.RestoreLayoutFromStream(New MemoryStream(ba))
            ServiceControl.PopupWindow.Width = node.GetInt32("PopupWidth", ServiceControl.PopupWindow.Width)
            ServiceControl.PopupWindow.Height = node.GetInt32("PopupHeight", ServiceControl.PopupWindow.Height)
            ServiceControl.UpdateConfiguration()
        End Sub

        Protected Overrides Sub SavePreferences(ByVal pref As IPreferencesNode)
            Dim node As IPreferencesNode
            Dim ms As MemoryStream

            ' clientControl
            node = pref.GetNodeForPath(ClientControl.Name)
            ms = New MemoryStream : ClientControl.GridView.SaveLayoutToStream(ms) : node.Item("Grid") = ms.ToArray()
            node("PopupWidth") = ClientControl.PopupWindow.Width
            node("PopupHeight") = ClientControl.PopupWindow.Height

            ' employeeControl
            node = pref.GetNodeForPath(EmployeeControl.Name)
            ms = New MemoryStream : EmployeeControl.GridView.SaveLayoutToStream(ms) : node.Item("Grid") = ms.ToArray()
            node("PopupWidth") = EmployeeControl.PopupWindow.Width
            node("PopupHeight") = EmployeeControl.PopupWindow.Height


            ' serviceControl
            node = pref.GetNodeForPath(ServiceControl.Name)
            ms = New MemoryStream : ServiceControl.GridView.SaveLayoutToStream(ms) : node.Item("Grid") = ms.ToArray()
            node("PopupWidth") = ServiceControl.PopupWindow.Width
            node("PopupHeight") = ServiceControl.PopupWindow.Height

            MyBase.SavePreferences(pref)
        End Sub
#End Region

#Region " Validating "
        Public Overrides Function DataValidate() As Boolean
            If Not MyBase.DataValidate() Then
                Return False
            End If

            If IsValidTime(DataSource.StartTime, DataSource.FinishTime, DataSource) Then
                Return True
            Else
                Return False
            End If

        End Function

        Private Function IsValidTime(ByVal start As DateTime, ByVal finish As DateTime, ByVal app As DataSetAppointments.Appointment) As Boolean
            If Not Helpers.AppointmentHelper.IsNotPastTime(app) Then
                ErrorProvider.SetError(AppointmentDate, Res.AppointmentBookInPastError)
                Return False
            End If

            If Not Helpers.AppointmentHelper.IsValidSalonTime(app, True) Then
                ErrorProvider.SetError(StartTime, Res.AppointmentNotInSalonTime)
                Return False
            End If
            If Not Helpers.AppointmentHelper.IsValidEmployeeTime(app, True) Then
                ErrorProvider.SetError(EmployeeControl, Res.AppointmentNotInStaffMemberFreeTime)
                Return False
            End If

            If Not Helpers.AppointmentHelper.IsValidClientTime(app, True) Then
                ErrorProvider.SetError(ClientControl, Res.AppointmentNotInClientFreeTime)
                Return False
            End If

            Return True
        End Function


        Private Sub ValidateFieldControl(ByVal c As FieldEditBase, ByVal err As String, ByVal e As System.ComponentModel.CancelEventArgs)
            If c.EditValue Is Nothing Then
                ErrorProvider.SetError(c, err)
                e.Cancel = True
            Else
                ErrorProvider.SetError(c, String.Empty)
            End If
        End Sub


        Private Sub ClientControl_DataValidating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
            If SuspendDataChangedEvents Then Return
            ValidateFieldControl(ClientControl, Res.AppointmentClientRequired, e)
        End Sub

        Private Sub EmployeeControl_DataValidating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
            If SuspendDataChangedEvents Then Return
            ValidateFieldControl(EmployeeControl, Res.AppointmentStaffMemberRequired, e)
        End Sub

        Private Sub ServiceControl_DataValidating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
            If SuspendDataChangedEvents Then Return
            ValidateFieldControl(ServiceControl, Res.AppointmentServiceRequired, e)
        End Sub


        Private Sub DurationTime_DataValidating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
            If SuspendDataChangedEvents Then Return

            If CInt(DurationTime.EditValue) < 1 Then
                e.Cancel = True
                ErrorProvider.SetError(DurationTime, Res.AppointmentInvaidDuration)
            Else
                ErrorProvider.SetError(DurationTime, String.Empty)
            End If
        End Sub

        Private Sub AppointmentDate_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles AppointmentDate.Validating
            If SuspendDataChangedEvents Then Return
            If Not AppointmentHelper.IsNotPastTime(DataSource) Then
                ErrorProvider.SetError(AppointmentDate, Res.AppointmentBookInPastError)
            Else
                ErrorProvider.SetError(AppointmentDate, String.Empty)
            End If
        End Sub

        Private Sub AppointmentDate_EditValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles AppointmentDate.EditValueChanged
            If SuspendDataChangedEvents Then Return

            If Not AppointmentHelper.IsNotPastTime(DataSource) Then
                ErrorProvider.SetError(AppointmentDate, Res.AppointmentBookInPastError)
                Return
            End If

            ErrorProvider.SetError(AppointmentDate, String.Empty)

            StartTime.Time = AppointmentDate.DateTime.Add(StartTime.Time.TimeOfDay)
            FinishTime.Time = AppointmentDate.DateTime.Add(FinishTime.Time.TimeOfDay)
        End Sub

#End Region

#Region " Data Exchange "
        Private Sub UpdateTimesOnForm()
            SuspendDataChangedEvents = True
            AppointmentDate.DateTime = DataSource.StartTime.Date
            StartTime.Time = DataSource.StartTime
            FinishTime.Time = DataSource.FinishTime
            DurationTime.EditValue = DataSource.FinishTime.Subtract(DataSource.StartTime).TotalMinutes

            Dim ed As Integer = AppointmentHelper.ExpectedServiceDuration(DataSource)
            If ed <> DurationTime.EditValue Then
                DurationInfo.Text = String.Format("Expected duration: {0} mins", ed)
                DurationInfo.Visible = True
            Else
                DurationInfo.Visible = False
            End If

            SuspendDataChangedEvents = False
        End Sub


        Private Sub EmployeeControl_EmployeeChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EmployeeControl.EditValueChanged
            UpdateTimeControls()
            If SuspendDataChangedEvents() Then Return
            If EmployeeControl.EditValue Is Nothing Then
                DataSource.SetPerformerIDNull()
            Else
                DataSource.PerformerID = CType(EmployeeControl.EditValue, DataSetData.Employee).EmployeeID
            End If
            UpdateCostAndDuration()
        End Sub

        Private Sub ServiceControl_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ServiceControl.EditValueChanged
            UpdateTimeControls()
            If SuspendDataChangedEvents() Then Return
            If ServiceControl.EditValue Is Nothing Then
                DataSource.SetServiceIDNull()
            Else
                DataSource.ServiceID = CType(ServiceControl.EditValue, DataSetData.Service).ServiceID
            End If
            UpdateDescription()
            UpdateCostAndDuration()
        End Sub

        Private Sub ClientControl_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClientControl.EditValueChanged
            If SuspendDataChangedEvents() Then Return
            If ClientControl.EditValue Is Nothing Then
                DataSource.SetClientIDNull()
            Else
                DataSource.ClientID = CType(ClientControl.EditValue, DataSetData.Client).ClientID
            End If
            UpdateDescription()
        End Sub


        Private Sub StartTime_EditValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles StartTime.EditValueChanged
            If SuspendDataChangedEvents() Then Return

            DataSource.StartTime = AppointmentDate.DateTime.Add(StartTime.Time.TimeOfDay)

            If DataSource.IsServiceIDNull Or DataSource.IsPerformerIDNull Then
                SuspendDataChangedEvents = True
                FinishTime.Time = StartTime.Time
                DurationTime.EditValue = 0
                SuspendDataChangedEvents = False

                DataSource.FinishTime = DataSource.StartTime
                DataSource.ExpectedDuration = 0
            Else
                DataSource.FinishTime = DataSource.StartTime.AddMinutes(DurationTime.EditValue)
            End If

            UpdateTimesOnForm()
        End Sub


        Private Sub FinishTime_EditValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles FinishTime.EditValueChanged
            If SuspendDataChangedEvents() Then Return

            DataSource.FinishTime = AppointmentDate.DateTime.Add(FinishTime.Time.TimeOfDay)

            UpdateTimesOnForm()
        End Sub

        Private Sub DurationTime_EditValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DurationTime.EditValueChanged
            If SuspendDataChangedEvents() Then Return

            DataSource.FinishTime = DataSource.StartTime.AddMinutes(DurationTime.EditValue)

            UpdateTimesOnForm()
        End Sub


        Private Sub UpdateTimeControls() 'ByVal sender As Object, ByVal e As System.EventArgs) Handles ServiceControl.EditValueChanged, EmployeeControl.EditValueChanged
            If (ServiceControl.EditValue Is Nothing) Or (EmployeeControl.EditValue Is Nothing) Then
                FinishTime.Enabled = False
                DurationTime.Enabled = False
            Else
                FinishTime.Enabled = True
                DurationTime.Enabled = True
            End If
        End Sub

        Private Sub UpdateDescription()
            If SuspendDataChangedEvents Then Return

            If (ServiceControl.EditValue Is Nothing) Or (ClientControl.EditValue Is Nothing) Then
                DataSource.Description = String.Empty
                DescriptionBox.Text = String.Empty
            Else
                Dim s As String = String.Format("{0} ({1})", _
                    CType(ClientControl.EditValue, DataSetData.Client).FullName, _
                    CType(ServiceControl.EditValue, DataSetData.Service).Label)

                DataSource.Description = s
                DescriptionBox.Text = s
            End If

        End Sub

        Private Sub UpdateCostAndDuration() 'ByVal sender As System.Object, ByVal e As System.EventArgs) 'Handles ServiceControl.EditValueChanged, EmployeeControl.EditValueChanged
            If SuspendDataChangedEvents Then Return
            If (ServiceControl.EditValue Is Nothing) Or (EmployeeControl.EditValue Is Nothing) Then
                DataSource.ExpectedDuration = 0
                DataSource.ServiceCost = 0
            Else
                DataSource.ExpectedDuration = AppointmentHelper.ExpectedServiceDuration(DataSource)
                DataSource.ServiceCost = AppointmentHelper.ExpectedServiceCost(DataSource)
                DataSource.FinishTime = DataSource.StartTime.AddMinutes(DataSource.ExpectedDuration)
            End If

            UpdateTimesOnForm()
        End Sub

        Private Sub Description_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DescriptionBox.TextChanged
            If SuspendDataChangedEvents Then Return

            Text = Title
        End Sub


#End Region

    End Class

End Namespace